package cloud

import (
	"github.com/astaxie/beego/logs"
	"smart_home/api/models"
	"smart_home/api/xfyun/depots"
)

func init() {
	registerCloud("trip_intent", trip_intent)
}

/*
提醒我
提醒我{content}
提醒我{time}{content}
提醒{person}
提醒{person}{content}
提醒{person}{time}{content}
{time}提醒我
{time}提醒{person}
{time}提醒我{content}
{time}提醒{person}{content}
*/
func trip_intent(rq *depots.Request, res *depots.XfyunResult, userParams *UserParams) {
	var (
		content, tm, person *depots.RSlot
		is                  bool
		err                 error
		toUser              *models.User
	)

	// 提醒人
	person = rq.GetSlot("person")

	// 提醒时间
	if tm = rq.GetSlot("time"); tm == nil {
		res.Answer = "请问，您想定几点的提醒呢"
		res.Slot = "time"
		return
	}

	// 提醒事件
	if content = rq.GetSlot("content"); content == nil {
		res.Answer = "需要提醒什么事呢"
		res.Slot = "content"
		return
	}

	// 判断提醒对象是否为家人
	if person != nil {
		if toUser, err = models.GetUserByName(person.Value); err != nil {
			logs.Error(err)
			return
		}
		if toUser == nil {
			res.Answer = person.Value + "不是您的家人"
			return
		}
		if is, err = models.IsMyFamily(toUser.Id, userParams.UserId); err != nil {
			logs.Error(err)
			return
		}
		if !is {
			res.Answer = person.Value + "不是您的家人"
			return
		}
	} else {
		toUser = userParams.User
	}

	res.Data["to"] = toUser.Id
	res.Data["what"] = content.Value
	res.IsHandle = true

	if toUser.Id == userParams.UserId {
		res.Answer = "好的，" + tm.Value + "提醒您" + content.Value
	} else {
		res.Answer = "好的，" + tm.Value + "帮您提醒" + toUser.NickName + content.Value
	}
}
